local blaze = require "blaze"
local game = require "club.threecard.sudoku"

local AvatarIcon = require "ui.Common.AvatarIcon"

local M = game.ui.element()

function M:ctor(data)
    self.data = data
    self.avatar_icon = AvatarIcon.new()
end

function M:on_injected(bind)
    bind("button.click", self.vs_btn, self.on_detail_click)
    bind("button.click", self.continue_btn, self.on_detail_click)
    bind("button.click", self.win_btn, self.on_detail_click)
    bind("button.click", self.lose_btn, self.on_detail_click)
    bind("gameobject.element", self.avatar_obj, self.avatar_icon)
end

function M:on_scroll_cell_update(data, index)
    self.data = data
    local my_id = game.kbe.player().user_id
    local result = game.util.versus.analyze(data, my_id)
    local target = result.target
    local win = result.win
    local draw = result.draw
    local lose = result.lose

    self.avatar_icon:set_gender(target.gender)
    self.avatar_icon:set_head(target.head)
    self.name_txt.text = target.nickname

    self.win_txt.text = tostring(win)
    self.draw_txt.text = tostring(draw)
    self.lose_txt.text = tostring(lose)
    self.vs_btn.gameObject:SetActive(data.status == 0)
    self.continue_btn.gameObject:SetActive(data.status == 1)
    self.win_btn.gameObject:SetActive(data.status == 2 and win > lose)
    self.lose_btn.gameObject:SetActive(data.status == 2 and win <= lose)
end

function M:on_detail_click()
    game.audio.post("click")
    local versus = self.data
    game.router.push("/club.threecard.sudoku/versus-detail/" .. versus.id)
end

return M